package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.spec.ServiceOfferVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.ServiceOfferMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DevChannelNameAttrHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {

        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(KiteWebContext.getContext().getCurrentSceneInstId());
        String serviceOfferId = sceneInst.getServiceOfferId();
        String rootInstId = kiteSceneInst.getRootInstId();
        String custId = KiteWebContext.getContext().getCust().getCustId();
        String cateId = querySceneInstService.getOfferInstAttrValue(custId, rootInstId, "cateId");
        KiteOfferInst giftOfferInst = querySceneInstService.getGiftOfferInst(sceneInstId);

        String prodId = "";
        if (Objects.nonNull(giftOfferInst)) {
            List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(custId, giftOfferInst.getOfferInstId());
            for (KiteProdInst prodInst : prodInstList) {
                prodId = prodInst.getProdId();
            }
        } else {
            KiteOfferInst mainOfferInst = querySceneInstService.getMainOfferInst(sceneInstId);
            if (Objects.nonNull(mainOfferInst)) {
                List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(custId, mainOfferInst.getOfferInstId());
                for (KiteProdInst prodInst : prodInstList) {
                    prodId = prodInst.getProdId();
                }
            }
        }
        ServiceOfferVO serviceOfferVO = this.getServiceOffer(serviceOfferId, cateId);
        if (Objects.isNull(serviceOfferVO)) {
            serviceOfferVO = this.getServiceOffer(serviceOfferId, prodId);
        }
        if (Objects.nonNull(serviceOfferVO) && KeyConsts.INTERNET_THINGS_BUSINESS.equals(serviceOfferVO.getOperateType())) {
            commonAttr.setIsEdit(KeyConsts.IFTRUE_F);
            setCommonAttr(commonAttr);
        }

    }


    private ServiceOfferVO getServiceOffer(String serviceOfferId, String cateId) {
        ServiceOfferMapper mapper = SpringUtil.getBean(ServiceOfferMapper.class);
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("offerId", cateId);
        requestMap.put("serviceOfferId", serviceOfferId);
        return mapper.getOfferService(requestMap).stream().findFirst().orElse(null);
    }

}
